{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# Only need to do this once...\n",
    "!pip install flask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "import json\n",
    "import re\n",
    "from flask import Flask, request, jsonify, make_response\n",
    "\n",
    "from google.cloud import datastore"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "app = Flask(__name__)\n",
    "\n",
    "\n",
    "@app.route('/webhook/', methods=['POST'])\n",
    "def handle():\n",
    "    req = request.get_json(silent=True, force=True)\n",
    "    print 'Request:'\n",
    "    print(json.dumps(req, indent=4))\n",
    "    if req.get('queryResult').get('action') != 'lookup':\n",
    "        return {}\n",
    "    topic = req.get('queryResult').get('parameters').get('topic')\n",
    "    topic = re.sub(r'[^\\w\\s]', '', topic)\n",
    "    print topic\n",
    "    rsp = getResponse(topic)\n",
    "    rsp = json.dumps(rsp, indent=4)\n",
    "    print rsp\n",
    "    r = make_response(rsp)\n",
    "    r.headers['Content-Type'] = 'application/json'\n",
    "    return r\n",
    "\n",
    "def getResponse(topic):\n",
    "    \n",
    "    client = datastore.Client()\n",
    "    query = client.query(kind='Synonym')\n",
    "    key = client.key('Synonym', topic)\n",
    "    query.key_filter(key, '=')\n",
    "    results = list(query.fetch())\n",
    "    \n",
    "    if len(results) == 0:\n",
    "        return buildReply('I can\\'t find that in the handbook...')\n",
    "    \n",
    "    print results[0]['synonym']\n",
    "    \n",
    "    query = client.query(kind='Topic')\n",
    "    key = client.key('Topic', results[0]['synonym'])\n",
    "    query.key_filter(key, '=')\n",
    "    results = list(query.fetch())\n",
    "    \n",
    "    print results[0]['action_text']\n",
    "    \n",
    "    return buildReply(results[0]['action_text'])\n",
    "\n",
    "def buildReply(info):\n",
    "    return {\n",
    "        'fulfillmentText': info,\n",
    "    }\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    app.run(host='0.0.0.0')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.15"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
